package com.example.dw.admin.controller;

import com.example.dw.common.Result;
import com.example.dw.common.ResultCode;
import com.example.dw.admin.dto.PageRequestDTO;
import com.example.dw.admin.dto.CreateUserDTO;
import com.example.dw.admin.dto.UpdateUserDTO;
import com.example.dw.admin.service.AdminUserService;
import com.example.dw.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管理端用户管理控制器
 * @author system
 * @since 2024-01-01
 */
@Tag(name = "管理端用户管理", description = "用户、教师、学生信息管理接口")
@Slf4j
@RestController
@RequestMapping("/admin")
@Validated
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    // ========== 用户信息管理 ==========

    /**
     * 分页获取用户信息
     */
    @Operation(summary = "分页获取用户信息", description = "分页查询所有用户信息")
    @PostMapping("/user/page")
    public Result<?> getUserByPage(@RequestBody @Validated PageRequestDTO pageRequest, 
                                 HttpServletRequest request) {
        try {
            Map<String, Object> result = adminUserService.getUserByPage(pageRequest);
            return Result.success("获取用户列表成功", result);
        } catch (Exception e) {
            log.error("分页获取用户信息失败: {}", e.getMessage());
            return Result.error("获取用户列表失败");
        }
    }

    /**
     * 获取用户信息
     */
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户详细信息")
    @GetMapping("/user/{userid}")
    public Result<?> getUser(@PathVariable Integer userid) {
        try {
            User user = adminUserService.getUserById(userid);
            return Result.success("获取用户信息成功", user);
        } catch (Exception e) {
            log.error("获取用户信息失败, userId: {}, 错误: {}", userid, e.getMessage());
            return Result.error("获取用户信息失败");
        }
    }

    /**
     * 创建用户
     */
    @Operation(summary = "创建用户", description = "创建新的用户账号")
    @PostMapping("/user")
    public Result<?> createUser(@RequestBody @Validated CreateUserDTO createUserDTO) {
        try {
            adminUserService.createUser(createUserDTO);
            return Result.success("创建用户成功");
        } catch (Exception e) {
            log.error("创建用户失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新用户信息
     */
    @Operation(summary = "更新用户信息", description = "更新用户的基本信息")
    @PutMapping("/user")
    public Result<?> updateUser(@RequestBody @Validated UpdateUserDTO updateUserDTO) {
        try {
            adminUserService.updateUser(updateUserDTO);
            return Result.success("更新用户信息成功");
        } catch (Exception e) {
            log.error("更新用户信息失败: {}", e.getMessage());
            return Result.error("更新用户信息失败");
        }
    }

    /**
     * 删除用户
     */
    @Operation(summary = "删除用户", description = "删除指定的用户")
    @DeleteMapping("/user")
    public Result<?> deleteUser(@RequestParam Integer userid) {
        try {
            adminUserService.deleteUser(userid);
            return Result.success("删除用户成功");
        } catch (Exception e) {
            log.error("删除用户失败, userId: {}, 错误: {}", userid, e.getMessage());
            return Result.error("删除用户失败");
        }
    }

    /**
     * 获取所有用户
     */
    @Operation(summary = "获取所有用户", description = "根据角色获取全部用户")
    @GetMapping("/user")
    public Result<?> getAllUser(@RequestParam(required = false) Integer role) {
        try {
            List<User> users = adminUserService.getAllUser(role);
            return Result.success("获取用户列表成功", users);
        } catch (Exception e) {
            log.error("获取所有用户失败: {}", e.getMessage());
            return Result.error("获取用户列表失败");
        }
    }

    // ========== 教师信息管理 ==========

    /**
     * 分页获取教师信息
     */
    @Operation(summary = "分页获取教师信息", description = "分页查询教师信息")
    @PostMapping("/teacher/page")
    public Result<?> getTeacherByPage(@RequestBody @Validated PageRequestDTO pageRequest) {
        try {
            Map<String, Object> result = adminUserService.getTeacherByPage(pageRequest);
            return Result.success("获取教师列表成功", result);
        } catch (Exception e) {
            log.error("分页获取教师信息失败: {}", e.getMessage());
            return Result.error("获取教师列表失败");
        }
    }

    /**
     * 获取教师信息
     */
    @Operation(summary = "获取教师信息", description = "根据教师ID获取教师详细信息")
    @GetMapping("/teacher/{userid}")
    public Result<?> getTeacher(@PathVariable Integer userid) {
        try {
            User teacher = adminUserService.getTeacherById(userid);
            return Result.success("获取教师信息成功", teacher);
        } catch (Exception e) {
            log.error("获取教师信息失败, userId: {}, 错误: {}", userid, e.getMessage());
            return Result.error("获取教师信息失败");
        }
    }

    /**
     * 创建教师
     */
    @Operation(summary = "创建教师", description = "创建新的教师账号")
    @PostMapping("/teacher")
    public Result<?> createTeacher(@RequestBody @Validated CreateUserDTO createUserDTO) {
        try {
            createUserDTO.setRole(1); // 设置为教师角色
            adminUserService.createUser(createUserDTO);
            return Result.success("创建教师成功");
        } catch (Exception e) {
            log.error("创建教师失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新教师信息
     */
    @Operation(summary = "更新教师信息", description = "更新教师的基本信息")
    @PutMapping("/teacher")
    public Result<?> updateTeacher(@RequestBody @Validated UpdateUserDTO updateUserDTO) {
        try {
            adminUserService.updateUser(updateUserDTO);
            return Result.success("更新教师信息成功");
        } catch (Exception e) {
            log.error("更新教师信息失败: {}", e.getMessage());
            return Result.error("更新教师信息失败");
        }
    }

    /**
     * 删除教师
     */
    @Operation(summary = "删除教师", description = "删除指定的教师")
    @DeleteMapping("/teacher")
    public Result<?> deleteTeacher(@RequestParam Integer userid) {
        try {
            adminUserService.deleteUser(userid);
            return Result.success("删除教师成功");
        } catch (Exception e) {
            log.error("删除教师失败, userId: {}, 错误: {}", userid, e.getMessage());
            return Result.error("删除教师失败");
        }
    }

    // ========== 学生信息管理 ==========

    /**
     * 分页获取学生信息
     */
    @Operation(summary = "分页获取学生信息", description = "分页查询学生信息")
    @PostMapping("/student/page")
    public Result<?> getStudentByPage(@RequestBody @Validated PageRequestDTO pageRequest) {
        try {
            Map<String, Object> result = adminUserService.getStudentByPage(pageRequest);
            return Result.success("获取学生列表成功", result);
        } catch (Exception e) {
            log.error("分页获取学生信息失败: {}", e.getMessage());
            return Result.error("获取学生列表失败");
        }
    }

    /**
     * 获取学生信息
     */
    @Operation(summary = "获取学生信息", description = "根据学生ID获取学生详细信息")
    @GetMapping("/student/{userid}")
    public Result<?> getStudent(@PathVariable Integer userid) {
        try {
            User student = adminUserService.getStudentById(userid);
            return Result.success("获取学生信息成功", student);
        } catch (Exception e) {
            log.error("获取学生信息失败, userId: {}, 错误: {}", userid, e.getMessage());
            return Result.error("获取学生信息失败");
        }
    }

    /**
     * 创建学生
     */
    @Operation(summary = "创建学生", description = "创建新的学生账号")
    @PostMapping("/student")
    public Result<?> createStudent(@RequestBody @Validated CreateUserDTO createUserDTO) {
        try {
            createUserDTO.setRole(0); // 设置为学生角色
            adminUserService.createUser(createUserDTO);
            return Result.success("创建学生成功");
        } catch (Exception e) {
            log.error("创建学生失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新学生信息
     */
    @Operation(summary = "更新学生信息", description = "更新学生的基本信息")
    @PutMapping("/student")
    public Result<?> updateStudent(@RequestBody @Validated UpdateUserDTO updateUserDTO) {
        try {
            adminUserService.updateUser(updateUserDTO);
            return Result.success("更新学生信息成功");
        } catch (Exception e) {
            log.error("更新学生信息失败: {}", e.getMessage());
            return Result.error("更新学生信息失败");
        }
    }

    /**
     * 删除学生
     */
    @Operation(summary = "删除学生", description = "删除指定的学生")
    @DeleteMapping("/student")
    public Result<?> deleteStudent(@RequestParam Integer userid) {
        try {
            adminUserService.deleteUser(userid);
            return Result.success("删除学生成功");
        } catch (Exception e) {
            log.error("删除学生失败, userId: {}, 错误: {}", userid, e.getMessage());
            return Result.error("删除学生失败");
        }
    }
} 